import router from 'umi/router';
import { message } from 'antd';
import { queryAllDistrict, saveDistrictAdd, saveDistrictEdit, saveDistrictDel } from '../services/district';


export default {
  namespace: 'district',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    loading: {
      reslist: true,
      model_submit:false,
    },
  },

  effects: {
    // 部门列表
    *fetch_district_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{reslist:true}
      })
      const response =yield call(queryAllDistrict, payload);
      response &&  (
        yield put({
          type: 'appendData',
          payload:response.data,
        })
      )
      yield put({
        type: 'changeLoading',
        payload:{reslist:false}
      })
    },

    // 删除部门
    *action_district_del({ payload }, { call, put }) {
        const response =yield call(saveDistrictDel, payload);
    },


    // 添加 部门信息
    *add_district({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{model_submit:true}
      })
      const response =yield call(saveDistrictAdd, payload);
      yield put({
        type: 'changeLoading',
        payload:{model_submit:false}
      })
      // response &&  router.push(`/system/district/list`)
    },


    // 编辑
    *edit_district({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload:{model_submit:true}
      })
      const response =yield call(saveDistrictEdit, payload);
      yield put({
        type: 'changeLoading',
        payload:{model_submit:false}
      })
    },


  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{
          list: [],
        },
        loading:{}
      };
    },
  },
};
